package com.ruoyi.erecord.samplebox.service.impl;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.erecord.samplebox.domain.Samplebox;
import com.ruoyi.erecord.samplebox.domain.bo.SampleboxBo;
import com.ruoyi.erecord.samplebox.domain.bo.SampleboxImportDetailBo;
import com.ruoyi.erecord.samplebox.domain.vo.SampleboxImportDetailVo;
import com.ruoyi.erecord.samplebox.domain.vo.SampleboxVo;
import com.ruoyi.erecord.samplebox.mapper.SampleboxMapper;
import com.ruoyi.erecord.samplebox.service.ISampleboxImportDetailService;
import com.ruoyi.erecord.samplebox.service.ISampleboxService;

import cn.hutool.core.bean.BeanUtil;
import lombok.RequiredArgsConstructor;

/**
 * 样本框Service业务层处理
 *
 * @author cc
 * @date 2023-04-21
 */
@RequiredArgsConstructor
@Service
public class SampleboxServiceImpl implements ISampleboxService {

	private final SampleboxMapper baseMapper;
	private final ISampleboxImportDetailService sampleboxImportDetailService;

	/**
	 * 查询样本框
	 */
	@Override
	public SampleboxVo queryById(Long id) {
		return baseMapper.selectVoById(id);
	}

	/**
	 * 查询样本框列表
	 */
	@Override
	public TableDataInfo<SampleboxVo> queryPageList(SampleboxBo bo, PageQuery pageQuery) {
		LambdaQueryWrapper<Samplebox> lqw = buildQueryWrapper(bo);
		Page<SampleboxVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
		return TableDataInfo.build(result);
	}

	/**
	 * 查询样本框列表
	 */
	@Override
	public List<SampleboxVo> queryList(SampleboxBo bo) {
		LambdaQueryWrapper<Samplebox> lqw = buildQueryWrapper(bo);
		return baseMapper.selectVoList(lqw);
	}

	private LambdaQueryWrapper<Samplebox> buildQueryWrapper(SampleboxBo bo) {
		Map<String, Object> params = bo.getParams();
		LambdaQueryWrapper<Samplebox> lqw = Wrappers.lambdaQuery();
		lqw.eq(StringUtils.isNotBlank(bo.getSampleboxCode()), Samplebox::getSampleboxCode, bo.getSampleboxCode());
		lqw.like(StringUtils.isNotBlank(bo.getSampleboxName()), Samplebox::getSampleboxName, bo.getSampleboxName());
		lqw.eq(bo.getSampleboxType() != null, Samplebox::getSampleboxType, bo.getSampleboxType());
		lqw.eq(StringUtils.isNotBlank(bo.getRegion()), Samplebox::getRegion, bo.getRegion());
		lqw.eq(StringUtils.isNotBlank(bo.getStreet()), Samplebox::getStreet, bo.getStreet());
		lqw.eq(StringUtils.isNotBlank(bo.getCommunity()), Samplebox::getCommunity, bo.getCommunity());
		return lqw;
	}

	/**
	 * 新增样本框
	 */
	@Override
	public Boolean insertByBo(SampleboxBo bo) {
		Samplebox add = BeanUtil.toBean(bo, Samplebox.class);
		validEntityBeforeSave(add);
		boolean flag = baseMapper.insert(add) > 0;
		if (flag) {
			bo.setSampleboxCode(add.getSampleboxCode());
		}
		return flag;
	}

	/**
	 * 修改样本框
	 */
	@Override
	public Boolean updateByBo(SampleboxBo bo) {
		Samplebox update = BeanUtil.toBean(bo, Samplebox.class);
		validEntityBeforeSave(update);
		return baseMapper.updateById(update) > 0;
	}

	/**
	 * 保存前的数据校验
	 */
	private void validEntityBeforeSave(Samplebox entity) {
		// TODO 做一些数据校验,如唯一约束
		if (entity.getId() == null) {
			Object o = this.queryBySampleboxCode(entity.getSampleboxCode());
			if(null != o) {
				throw new ServiceException("样本框编码 已经存在了！");
			}
		}
	}

	/**
	 * 批量删除样本框
	 */
	@Override
	public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
		if (isValid) {
			// TODO 做一些业务上的校验,判断是否需要校验
		}
		return baseMapper.deleteBatchIds(ids) > 0;
	}

	@Override
	public Boolean isUseEmployeeId(Long employeeId) {
		LambdaQueryWrapper<Samplebox> lqw = Wrappers.lambdaQuery();
		lqw.eq(Samplebox::getCityPrincipal, employeeId).or().eq(Samplebox::getCitySteward, employeeId);
		return this.baseMapper.exists(lqw);
	}

	@Override
	public Boolean batchInsertOrUpdate(String batchNo) {
		SampleboxImportDetailBo queryBo = new SampleboxImportDetailBo();
		queryBo.setBatchNo(batchNo);
		List<SampleboxImportDetailVo> detailList = this.sampleboxImportDetailService.queryList(queryBo);
		List<Samplebox> list = BeanCopyUtils.copyList(detailList, Samplebox.class);
		list.forEach(e -> {
			e.restBase();
			SampleboxVo svo = this.queryBySampleboxCode(e.getSampleboxCode());
			if (null != svo) {
				e.setId(svo.getId());
			}
		});
		return this.baseMapper.insertOrUpdateBatch(list);
	}

	@Override
	public SampleboxVo queryBySampleboxCode(String sampleboxCode) {
		LambdaQueryWrapper<Samplebox> lqw = Wrappers.lambdaQuery();
		lqw.eq(Samplebox::getSampleboxCode, sampleboxCode);
		lqw.last("limit 1");
		return this.baseMapper.selectVoOne(lqw);
	}
}
